// 和用户相关的状态管理

import { createSlice } from "@reduxjs/toolkit";
import { removeToken, request } from "@/utils"
import { setToken as _setToken, getToken } from "@/utils"
import { getProfileAPI, loginAPI } from "@/apis/user";

const userStore = createSlice({
    name: 'user',
    // 数据状态
    initialState: {
        token: getToken() ?? '',
        userInfo: {}
    },
    // 同步修改方法
    reducers: {
        setToken(state, action) {
            state.token = action.payload
            // 往localstorage存一份
            _setToken(action.payload)
        },
        setUserInfo(state, action) {
            state.userInfo = action.payload
        },
        clearUserInfo(state) {
            state.token = ''
            state.userInfo = {}
            removeToken()
        }
    }
})

// 解构出actionCreater
const { setToken, setUserInfo, clearUserInfo } = userStore.actions

// 获取reducer函数
const userReducer = userStore.reducer

// 异步方法 完成登录获取token
const fetchLogin = (loginForm) => {
    return async (dispatch) => {
        // 1.发送异步请求
        await loginAPI(loginForm).then((res) => {
            // 2.提交同步action进行token的存入
            dispatch(setToken(res.data.data.token))
        }).catch((error) => {
            console.log(error);
        })
    }
}

// 异步方法 获取个人用户信息
const fetchUserInfo = () => {
    return async (dispatch) => {
        await getProfileAPI().then((res) => {
            dispatch(setUserInfo(res.data.data))
        }).catch((error) => {
            console.log(error.status);
        })
    }
}

export { setToken, fetchLogin, fetchUserInfo, clearUserInfo }

export default userReducer